package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import model.AlertParameters;
import utils.JdbcUtil;

public class AlertParametersDao {
	/**
	 * 根据ueId获取所有与该事件相关的信息，包括t_dbs,t_tables,t_fields,t_eventFieldsConditon,t_userEvent,t_user的所有信息
	 * @param ueId 事件ID
	 * @return
	 */
	public AlertParameters getAlertParameters(String ueId){
		Connection con =null;  
        PreparedStatement pstmt = null;  
        ResultSet rs = null;
        AlertParameters ap = null;
        try{
        	con = JdbcUtil.getCon();
			String sql = "select db.dbName, db.driverName, db.password, db.userName, "
					+ "db.url, t.tableName, f.fieldName, ue.customTime, condition, efc.value, "
					+ "ue.pollingTime, ue.timeSpan, u.phoneNumber,u.email , efc.locationValue "
					+ "from t_user u, t_userEvent ue, t_eventFieldsCondition efc, t_fields f, t_tables t, t_dbs db "
					+ "where u.id = ue.uId and ue.id = efc.ueId and f.id = efc.fId and t.id = f.tId and db.id = t.dbId "
					+ "and ue.id = " + ueId;
			//System.out.println(sql);
			pstmt = con.prepareStatement(sql);
			rs = pstmt.executeQuery();
			ap = new AlertParameters();
			while(rs.next()){
				ap.setPhone(rs.getString("phoneNumber"));
				ap.setEmail(rs.getString("email"));
				ap.setDbName(rs.getString("dbName"));
				ap.setUrl(rs.getString("url"));
				ap.setUserName(rs.getString("userName"));
				ap.setUserPwd(rs.getString("password"));
				ap.setDrvName(rs.getString("driverName"));
				ap.setTable(rs.getString("tableName"));
				ap.setField(rs.getString("fieldName"));
				ap.setCondition(rs.getString("condition"));
				ap.setInterval(rs.getInt("pollingTime"));
				ap.setTimeSpan(rs.getInt("timeSpan"));
				ap.setThreshold(rs.getString("value"));
				ap.setQuerycontent(rs.getString("fieldName"));
				ap.setCustomTime(rs.getString("customTime"));
				ap.setLocation(rs.getString("location"));
				ap.setLocationValue(rs.getString("locationValue"));
			}
		}catch(Exception e)        {
			e.printStackTrace();
        }finally{
        	JdbcUtil.closeAll(rs, pstmt,con);
        }
		return ap;
	}
}
